<template>
	<view style="padding-top: 140upx;">
		<view class="inputArea" style="text-align: center;">
			<image class="logo-img" src="../../static/images/money.jpg"></image>
		</view>
		<view class="text-xl padding text-center">
			<text class="text-white text-bold">记账簿</text>
		</view>
		<view class="text-s padding text-center">
			<text class="text-white">家常记账，知悭识俭</text>
		</view>

		<view class="inputArea">
			<button class="login-button " style="background-image: linear-gradient(to right, #ed6ea0 0%, #ec8c69 100%);" open-type="getUserInfo" @getuserinfo="goLogin">翻开记数簿</button>
		</view>
	</view>
</template>

<script>
	import {
		callCloudFunction
	} from '@/common/public_util.js';

	let _self;
	export default {
		components: {},
		data() {
			return {

			}
		},
		onLoad() {
			_self = this;
			
		},
		methods: {
			goLogin() {
				
				_self.getUserInfo();
			},
			
			getUserInfo(){
				uni.login({
					provider:'weixin',
					success: (loginRes) => {
						
						
						this.code=loginRes.code;
						
					}
				})
				
				
				uni.showModal({
					title: '温馨提示',
					content: '亲，授权微信登录后才能正常使用小程序功能',
					success(res) {
						//如果用户点击了确定按钮
						if (res.confirm) {
							uni.getUserProfile({
								desc: '获取你的昵称、头像、地区及性别',
								success: infoRes => {
									console.log(infoRes);
									
							
									callCloudFunction('get_openid', {
										code: _self.code
									}, (res) => {
										console.log(res);
										
										//获取到openid
										let openid = res.data.openid;
										let userInfo = infoRes.userInfo;
										userInfo.openid = openid;
							
										console.log(userInfo);
										uni.setStorageSync('userInfo', userInfo);
										callCloudFunction('user_add', userInfo, (res) => {},(err) => {},'登陆中...',false);

										uni.switchTab({
											url: '/pages/app/bookkeeping/bookkeeping',
											
										});
									},()=>{},'登陆中...',);
							
								},
								fail: res => {
							
									console.log(res)
									//拒绝授权
									uni.showToast({
										title: '您拒绝了请求,不能正常使用小程序',
										icon: 'error',
										duration: 2000
									});
									return;
								}
							});
						} else if (res.cancel) {
							//如果用户点击了取消按钮
							
							uni.showToast({
								title: '您拒绝了请求,不能正常使用小程序',
								icon: 'error',
								duration: 2000
							});
							return;
						}
					}
				});
			},
			
			
			
			
		}
	}
</script>

<style>
	.inputArea {
		padding: 30upx 10%;
	}

	.inputClass {
		border: 2px solid #ccc;
		border-radius: 44upx;
		outline: 0;
		width: 90%;
		padding: 16upx 30upx;
		background-color: #FFFFFF;
	}

	.logo-img {
		width: 150upx;
		height: 150upx;
		border-radius: 150upx;
	}

	.login-button {
		/* background: #2fc25b; */
		color: #FFFFFF;
		border-radius: 54upx;
		font-size: 36upx;
		text-align: center;
		padding: 20upx;
	}
</style>
